package com.hzjh.h5mall.ui.forgot

import com.alibaba.android.arouter.facade.annotation.Route
import com.hzjh.base.ext.R
import com.hzjh.base.ext.data.Configs
import com.hzjh.base.ext.frame.JHBaseActivity
import com.hzjh.base.ext.route.RouteUrl
import com.hzjh.base.router.ext.navigationTo
import com.hzjh.base.style.weight.ToastKt.toastCenter
import com.hzjh.base.utils.other.CommUtils
import com.hzjh.h5mall.BuildConfig
import com.hzjh.h5mall.databinding.ActivityForgotBinding
import com.hzjh.h5mall.model.forgot.ForgotVM

//忘记密码 助记词
@Route(path = RouteUrl.FORGOT_ACTIVITY)
class ForgotActivity : JHBaseActivity<ActivityForgotBinding,ForgotVM>() {
    private var isAgree = false

    override fun initData() {

    }

    override fun initView() {
        viewBinding.inputSrp.setTextChangeListener {
            changeLoginStatus()
        }
        viewBinding.inputPwd.setTextChangeListener {
            changeLoginStatus()
        }
        viewBinding.inputConfirmPwd.setTextChangeListener {
            changeLoginStatus()
        }

        viewBinding.btnNext.setOnClickListener {
            if(!isAgree){
                toastCenter(CommUtils.getString(R.string.jh_agree_policy_toast))
                return@setOnClickListener
            }
            val srps=viewBinding.inputSrp.text.toString()
            val pwd=viewBinding.inputPwd.text.toString()
            val confirmPwd=viewBinding.inputConfirmPwd.text.toString()
            //找回密码
            viewModel.reqResetPasswordBySrp(srps,pwd,confirmPwd)
        }

        viewBinding.ivCheckAgree.setOnClickListener {
            changeAgreeStatus()
        }
        viewBinding.tvCheckAgree.setOnClickListener {
            changeAgreeStatus()
        }
        viewBinding.tvUserAgreement.setOnClickListener {
            //用户协议
            navigationTo(RouteUrl.WEB_ACTIVITY){
                withString(RouteUrl.PARAMS_WEB_URL, BuildConfig.h5Url+ Configs.USER_AGREEMENT_URL)
                withBoolean(RouteUrl.PARAMS_WEB_BAR_VISIBILITY,true)
                withBoolean(RouteUrl.PARAMS_WEB_TITLE_BAR_VISIBILITY,true)
            }
        }
    }

    private fun changeLoginStatus(){
        viewBinding.btnNext.isEnabled = !viewBinding.inputSrp.text.isNullOrBlank() && !viewBinding.inputPwd.text.isNullOrBlank() && !viewBinding.inputConfirmPwd.text.isNullOrBlank()
    }
    private fun changeAgreeStatus(){
        isAgree = !isAgree
        if (isAgree) {
            viewBinding.ivCheckAgree.setImageResource(R.drawable.ic_checkbox_true)
        } else {
            viewBinding.ivCheckAgree.setImageResource(R.drawable.ic_checkbox_false)
        }
    }

    override fun initViewBinding(): ActivityForgotBinding {
        return ActivityForgotBinding.inflate(layoutInflater)
    }
}